<?php
namespace Test\Controller;
use Shop\Api\OrdersItemApi;
use Shop\Api\OrderWithItemsApi;
use Shop\Api\OrderWithStoresApi;
use Think\Controller\RestController;
use Api\Service\OAuth2ClientService;
/**
 * Created by PhpStorm.
 * User: an
 * Date: 2015/8/25
 * Time: 10:12
 */
class TestOrdersApiController extends RestController{

    public function __construct(){
        parent::__construct();

        $client_id = C('CLIENT_ID');
        $client_secret = C('CLIENT_SECRET');
        $config = array(
            'client_id'=>$client_id,
            'client_secret'=>$client_secret,
            'site_url'=>C("SITE_URL"),
        );
        $client = new OAuth2ClientService($config);
        $access_token = $client->getAccessToken();
        if($access_token['status']){
            $this->assign("access_token",$access_token['info']);
        }
        $this->assign("error",$access_token);
    }

    public function testItemQuery(){
        $this->display();
    }

    /**
     * Convert
     * @param $orderList
     * @param $itemList
     * @return mixed
     * @internal param $order_list
     */
    private function combine($orderList,$itemList){

        foreach($itemList as $vo){
            $order = &$orderList[$vo['order_code']];
            if(!empty($order)){
                array_push($order['_items'],$vo);
            }
        }

        return $orderList;
    }

    private function getMapFromResult(&$list){

        $newList = array();

        $mapItem = array();

        foreach($list as $vo){
            array_push($mapItem,$vo['order_code']);
            $newList[$vo['order_code']] = $vo;
            $newList[$vo['order_code']]['_items'] = array();
        }
        $list = $newList;
        return array('order_code'=>array('in',$mapItem));
    }
    /**
     * =
     */
    public function testQuery(){
        $this->display();
    }

    /**
     *
     */
    public function testDetail(){
        $this->display();
    }

    public function testAdd(){
        $this->display();
    }

    public function order(){
        $this->display();
    }

    public function testOrder(){
        $this->display();
    }

    public function testAddSingle(){
        $this->display();
    }

    /**
     * Convert
     * @param $order_list
     * @return mixed
     */
    private function convert($order_list){
        $converted_list = array();
        $count = $order_list['count'];
        if(is_array($order_list['list'])){
            foreach($order_list['list'] as &$vo){
                $vo['store_logo_url'] = ($vo['store_logo']);
                $vo['img_url'] = ($vo['img']);
                $entity = null;
                if(isset($converted_list[$vo['order_code']])){
                    $entity = $converted_list[$vo['order_code']];
                }else{
                    $entity = array(
                        'tax_amount'=>$vo['taxamount'],
                        'goods_amount'=>$vo['goodsamount'],
                        'id'=>$vo['id'],
                        'order_code'=>$vo['order_code'],
                        'post_price'=>$vo['post_price'],
                        'discount_money'=>$vo['discount_money'],
                        'createtime'=>$vo['createtime'],
                        'uid'=>$vo['uid'],
                        'price'=>$vo['price'],
                        'storeid'=>$vo['storeid'],
                        'pay_status'=>$vo['pay_status'],
                        'order_status'=>$vo['order_status'],
                        '_items'=>array(

                        )
                    );
                }

                $item = array(
                    'weight'=>$vo['weight'],
                    'p_id'=>$vo['p_id'],
                    'order_code'=>$vo['order_code'],
                    'count'=>$vo['count'],
                    'sku_desc'=>$vo['sku_desc'],
                    'sku_id'=>$vo['sku_id'],
                    'post_price'=>$vo['post_price'],
                    'has_sku'=>$vo['has_sku'],
                    'name'=>$vo['name'],
                    'img_url'=>$vo['img_url'],
                    'item_price'=>$vo['item_price'],
                    'ori_price'=>$vo['ori_price'],
                );

                array_push($entity['_items'],$item);

                $converted_list[$vo['order_code']] = $entity;

            }
        }

        return array('total'=>$count,'list'=>$converted_list);
    }

}
